import 'package:flutter/material.dart';
import 'package:screen_brightness/screen_brightness.dart';

class ZsnClass {
  Color baseColor = const Color.fromARGB(255, 0, 110, 255);
  ZsnClass();
  get baseColor1 => baseColor;
  // 计算时分秒
  String formatDuration(Duration duration) {
    String twoDigits(int n) => n.toString().padLeft(2, "0");
    String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60));
    String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60));
    return duration.inHours > 0
        ? "${twoDigits(duration.inHours)}:$twoDigitMinutes:$twoDigitSeconds"
        : "$twoDigitMinutes:$twoDigitSeconds";
  }

  Icon volumeIcon(double volume) {
    if (volume == 0) {
      return Icon(Icons.volume_off, color: Colors.grey);
    } else if (volume < 0.5) {
      return Icon(Icons.volume_down, color: Colors.blue);
    } else {
      return Icon(Icons.volume_up, color: Colors.blue);
    }
  }

  Icon brightnessIcon(double brightness) {
    if (brightness == 0) {
      return Icon(Icons.brightness_low, color: Colors.grey);
    } else if (brightness < 0.5) {
      return Icon(Icons.brightness_medium, color: Colors.blue);
    } else {
      return Icon(Icons.brightness_high, color: Colors.blue);
    }
  }

  // 获取应用亮度
  Future<double> get applicationBrightness async {
    try {
      return await ScreenBrightness.instance.application;
    } catch (e) {
      throw 'Failed to get application brightness';
    }
  }

  // 设置应用亮度
  Future<void> setApplicationBrightness(double brightness) async {
    try {
      await ScreenBrightness.instance
          .setApplicationScreenBrightness(brightness);
    } catch (e) {
      throw 'Failed to set application brightness';
    }
  }

  // 重置应用亮度
  Future<void> resetApplicationBrightness() async {
    try {
      await ScreenBrightness.instance.resetApplicationScreenBrightness();
    } catch (e) {
      throw 'Failed to reset application brightness';
    }
  }

  // 计算锁屏UI、亮度UI、音量UI的位置
  // top = 元素高度的一半  - 自身高度的一半（默认为按钮高度的一半）
  double calculateTopPosition(BuildContext context, {double height = 48.0}) {
    final screenHeight = MediaQuery.of(context).size.height;
    return (screenHeight / 2) - (height / 2);
  }
}
